<template>
  <div class="catemanager">
    <myheader title="频道管理">
      <template #left>
        <van-icon name="arrow-left" @click="$router.go(-1)" />
      </template>
      <template #center>
        <span>栏目管理</span>
      </template>
    </myheader>
    <div class="mychannel">
      <h3>点击删除频道</h3>
      <div class="list">
        <span
          v-for="(item, index) in cataList"
          :key="item.id"
          @click="remove(index, item)"
          >{{ item.name }}</span
        >
      </div>
    </div>
    <div class="youchannel">
      <h3>点击添加频道</h3>
      <div class="list">
        <span
          v-for="(item, index) in removeList"
          :key="item.id"
          @click="add(index, item)"
          >{{ item.name }}</span
        >
      </div>
    </div>
  </div>
</template>

<script>
import myheader from "@/components/myheader";
import { getCateList } from "@/api/categorys";
export default {
  data() {
    return {
      cataList: [],
      addList: [],
      removeList: [],
    };
  },
  components: {
    myheader,
  },
  async mounted() {
    let res = await getCateList();
    //console.log(res);
    this.cataList = res.data.data;
    //判断用户数是否登录状态  关注和头条不参与
    if (localStorage.getItem("userKey")) {
      this.cataList.splice(0, 2);
    } else {
      this.cataList.splice(0, 1);
    }
    //获取当前有的栏目
    if (localStorage.getItem("addlist")) {
      this.cataList = JSON.parse(localStorage.getItem("addlist"));
    }

    //判断是否有已经删除的栏目，如果有渲染 没有 就执行添加
    if (localStorage.getItem("removelist")) {
      this.removeList = JSON.parse(localStorage.getItem("removelist"));
    }
  },
  methods: {
    remove(index, item) {
      //移除栏目
      this.removeList.push(item);
      //将移除的栏目保存到本地,传到服务器的需要转换为json字符串
      localStorage.setItem("removelist", JSON.stringify(this.removeList));
      this.cataList.splice(index, 1);
      localStorage.setItem("addlist", JSON.stringify(this.cataList));
    },
    add(index, item) {
      //添加栏目
      this.cataList.push(item);
      //添加到本地
      localStorage.setItem("addlist", JSON.stringify(this.cataList));
      //移除删除频道
      this.removeList.splice(index, 1);
      //再次保存到本地，移出列表
      localStorage.setItem("removelist", JSON.stringify(this.removeList));
    },
  },
};
</script>

<style lang="less" scoped>
.mychannel,
.youchannel {
  padding: 10px;
  h3 {
    line-height: 30px;
    font-weight: bold;
  }
  .list {
    padding: 10px;
    display: flex;
    flex-wrap: wrap;
    > span {
      width: 20%;
      margin-left: 10px;
      margin-top: 10px;
      border: 1px solid #ccc;
      text-align: center;
      line-height: 40px;
    }
  }
}
</style>